package cn.telling.product.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.telling.product.dao.IProductBrandDao;
import cn.telling.product.vo.ProductBrandVO;
import cn.telling.utils.AutoInjection;
import cn.telling.utils.BaseUtil;
import cn.telling.utils.StringHelperTools;
/**
 * 
 * @ClassName: ProductBrandDaoImpl 
 * @Description: 产品品牌数据服务接口
 * @author chxb
 * @date 2015年9月26日 上午11:21:56 
 *
 */
@Repository("productBrandDao")
public class ProductBrandDaoImpl implements IProductBrandDao {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ProductBrandDaoImpl.class);

    @Autowired
    @Qualifier("film-template")
    private JdbcTemplate jdbcTemplate;

	@Override
	public List<ProductBrandVO> findPrarentBrandbySortId(String sortid) {
		String sql = "select * from PRODUCT_BRAND where sortid = ? and del_flag = '1' and parentid =0  and isvalid = '01' order by brandname ";
		Object[] params = new Object[]{sortid};
		params=StringHelperTools.nullToString(params);
		LOGGER.debug("通过产品类型id获取父品牌:"+BaseUtil.logSQL(sql, params));
		return getJdbcTemplate().query(sql,params,new RowMapper<ProductBrandVO>() {
			public ProductBrandVO mapRow(ResultSet rs, int rowNum) throws SQLException {
				ProductBrandVO vo = new ProductBrandVO();
				AutoInjection.Rs2Vo(rs, vo, null);
				return vo;
			}
		});
	}
	
	@Override
	public List<ProductBrandVO> findProductBrandByParentID(String prarentId) {
		String sql = "select * from PRODUCT_BRAND where PARENTID = ? and del_flag = '1'  and isvalid = '01' order by brandname ";
		Object[] params = new Object[]{prarentId};
		params=StringHelperTools.nullToString(params);
		LOGGER.debug("通过产品类型id获取二级品牌:"+BaseUtil.logSQL(sql, params));
		return getJdbcTemplate().query(sql,params,new RowMapper<ProductBrandVO>() {
			public ProductBrandVO mapRow(ResultSet rs, int rowNum) throws SQLException {
				ProductBrandVO vo = new ProductBrandVO();
				AutoInjection.Rs2Vo(rs, vo, null);
				return vo;
			}
		});
	}
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
}
